package com.wisemoresystem.seal.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_destory;
import com.wisemoresystem.seal.model.t_seal_destroy_verify;
import com.wisemoresystem.seal.model.t_seal_over_log;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class SealDestroyService extends BaseService {

	@Autowired
	private BankService bService;
	@Autowired
	private SealSetService sService;

	@Task(code = "seal_destruction10", tip = "未完成的印章上缴(@cnt)")
	public long selectSealDestroyApplyList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.flag_stop='上缴停用' and t.flag='停用' ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId(),
				WebTools.getCurrentUserId());
		return cnt;
	}

	// 上缴时列表
	public List<Map<String, Object>> selectSealDestroyApplyList(String bank_id,
			String srh_name, Page page) {

		String sql = "select t.*,tb.name bank_name,tm.title code,tc.name mac_model from t_seal t "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.flag_stop='上缴停用' "
				+ " and t.flag='停用' and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	@Task(code = "seal_distribution11", tip = "未完成的印章上缴接收(@cnt)")
	public long selectSealDestroyRecieveList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='上缴停用' and t.flag='上缴中' ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId());
		return cnt;
	}

	// 接收时列表
	public List<Map<String, Object>> selectSealDestroyRecieveList(
			String bank_id, String srh_name, Page page) {

		String sql = "select t.*,tb.name bank_name,tm.title code,tc.name mac_model from t_seal t "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='上缴停用' "
				+ " and t.flag='上缴中' and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	/*@Task(code = "seal_destruction1", tip = "未完成的印章销毁申请(@cnt)")
	public long selectSealDestroyList() {
		int lev = bService.getBankLevel(WebTools.getCurrentBankId());
		String hql = "select count(*) cnt from t_seal t "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " left join t_bank_level tl on tc.verify_destory=tl.name "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and "
				+ " ((t.flag_stop='暂时停用' and t.flag='停用' and tl.sort>=?) or (t.flag_stop='上缴停用' "
				+ " and t.flag='待销毁'))";
		Long cnt = super.getTaskCnt(hql, WebTools.getCurrentBankId(),
				WebTools.getCurrentUserId(), lev);
		return cnt;
	}*/

	// 销毁申请时列表
	public List<Map<String, Object>> selectSealDestroyList(String bank_id,
			String srh_name, Page page) {
		int lev = bService.getBankLevel(WebTools.getCurrentBankId());

		String sql = " select t.*,tb.name bank_name,tm.title code,tc.name mac_model from t_seal t "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " left join t_bank_level tl on tc.verify_destory=tl.name"
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.true_code like ? and "
				+ " ((t.flag_stop='暂时停用' and t.flag='停用' and tl.sort>=?) or (t.flag_stop='上缴停用' "
				+ " and t.flag='待销毁')) ";
		List<Object> params = new ArrayList<Object>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		params.add(lev);
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);

		return list;
	}

	@Task(code = "seal_destruction2", tip = "未完成的印章销毁(@cnt)")
	public long selectSealDestroyDestroyList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_destroy_verify tv on tv.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.flag_stop='上缴停用' and t.flag='销毁申请中' and tv.review_flag='通过' ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId(),
				WebTools.getCurrentUserId());
		return cnt;
	}

	// 销毁列表
	public List<Map<String, Object>> selectSealDestroyDestroyList(
			String bank_id, String srh_name, Page page) {

		String sql = "select t.*,tb.name bank_name,tm.title code,tc.name mac_model from t_seal t "
				+ " join t_seal_destroy_verify tv on tv.seal_id=t._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id=? and t.flag_stop='上缴停用' "
				+ " and t.flag='销毁申请中' and tv.review_flag='通过' and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	// 上缴
	@Transactional
	public boolean over(String seal_id, t_seal_over_log over_log) {

		t_seal tSeal = super.findObjById(seal_id, t_seal.class);
		tSeal.setBank_id(over_log.getVerify_bank_id());
		tSeal.setFlag("上缴中");
		super.updateObj(tSeal);
		// 保存印章流转记录
		over_log.setApply_user_id(WebTools.getCurrentUserId());
		over_log.setApply_time(getToday());
		over_log.setFlag("在途");
		super.saveObj(over_log);

		return true;
	}

	// 接收
	@Transactional
	public boolean recieve(String seal_id, t_seal_over_log over_log,
			String save_user_id) {

		t_seal tSeal = super.findObjById(seal_id, t_seal.class);
		t_seal_over_log log = selectOverLogByIDs(seal_id,
				WebTools.getCurrentBankId());
		tSeal.setBank_id(WebTools.getCurrentBankId());
		// 判断盖章是否在销毁权限之上
		if (sService.checkIsTop(seal_id)) {
			tSeal.setFlag("待销毁");
			tSeal.setUse_bank_id(WebTools.getCurrentBankId());
		} else {
			tSeal.setFlag("停用");
		}
		tSeal.setSave_user_id(save_user_id);
		tSeal.setRecieve_user_id(WebTools.getCurrentUserId());
		super.updateObj(tSeal);

		// 保存印章流转记录
		log.setVerify_user_id(WebTools.getCurrentUserId());
		log.setVerify_time(getToday());
		log.setVerify_note(over_log.getVerify_note());
		log.setFlag("已接收");
		super.updateObj(log);

		return true;
	}

	// 申请
	@Transactional
	public boolean apply(String seal_id, t_seal_destory destroy) {

		t_seal tSeal = super.findObjById(seal_id, t_seal.class);
		tSeal.setFlag("销毁申请中");
		tSeal.setFlag_stop("上缴停用");
		super.updateObj(tSeal);

		destroy.setUser_id(WebTools.getCurrentUserId());
		destroy.setBank_id(WebTools.getCurrentBankId());
		destroy.setApply_time(getToday());
		super.saveObj(destroy);

		t_seal_destroy_verify verify = new t_seal_destroy_verify();
		verify.setSeal_id(seal_id);
		super.saveObj(verify);

		return true;
	}

	// 销毁
	@Transactional
	public boolean destroy(String seal_id, t_seal_destory destroy) {

		t_seal tSeal = super.findObjById(seal_id, t_seal.class);
		tSeal.setFlag("已销毁");
		super.updateObj(tSeal);

		t_seal_destory t = selectDestroyByIDs(seal_id,
				WebTools.getCurrentBankId());
		t.setVerify_user_id(WebTools.getCurrentUserId());
		t.setVerify_time(getToday());
		t.setVerify_note(destroy.getVerify_note());
		t.setWitness(destroy.getWitness());
		t.setFile_id(destroy.getFile_id());
		super.saveObj(t);

		return true;
	}

	private Timestamp getToday() {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return DateTools.strToDatestamp(df.format(new Date()));
	}

	public t_seal_over_log selectOverLogByIDs(String seal_id, String bank_id) {
		String hql = "from t_seal_over_log t where t.status=0 and t.seal_id=? "
				+ " and t.verify_bank_id=? and t.flag='在途' ";
		t_seal_over_log log = super.findObj(hql, seal_id, bank_id);
		return log;
	}

	public t_seal_destory selectDestroyByIDs(String seal_id, String bank_id) {
		String hql = "from t_seal_destory t where t.status=0 and t.seal_id=? "
				+ " and t.bank_id=? ";
		t_seal_destory t = super.findObj(hql, seal_id, bank_id);
		return t;
	}

	@Task(code = "check_destroy", tip = "未完成的印章销毁审核(@cnt)")
	public long selectSealDestroyCheckList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_destroy_verify tv on tv.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and t.flag_stop='上缴停用' and t.flag='销毁申请中' and tv.check_flag is null ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId(),
				WebTools.getCurrentUserId());
		return cnt;
	}

	// 审核
	public List<Map<String, Object>> selectSealDestroyCheckList(String bank_id,
			String srh_name, Page page) {

		String sql = "select t.*,tb.name bank_name,tm.title code,tc.name mac_model,tv._id v_id from t_seal_destroy_verify tv "
				+ " join t_seal t on tv.seal_id=t._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and t.flag_stop='上缴停用' "
				+ " and t.flag='销毁申请中' and tv.check_flag is null and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}

		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	@Task(code = "review_destroy", tip = "未完成的印章销毁复核(@cnt)")
	public long selectSealDestroyReviewList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_destroy_verify tv on tv.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and tv.check_user_id!=? and t.flag_stop='上缴停用' and t.flag='销毁申请中' and tv.check_flag='通过' and tv.review_flag is null ";

		Long cnt = super.getTaskCnt(sql, WebTools.getCurrentBankId(),
				WebTools.getCurrentUserId(), WebTools.getCurrentUserId());
		return cnt;
	}

	// 复审
	public List<Map<String, Object>> selectSealDestroyReviewList(
			String bank_id, String srh_name, Page page) {

		String sql = "select t.*,tb.name bank_name,tm.title code,tc.name mac_model,tv._id v_id from t_seal t "
				+ " join t_seal_destroy_verify tv on tv.seal_id=t._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_seal_new tm on t.seal_new_id=tm._id "
				+ "	left join t_seal_type_config tc on tm.seal_type_config_id=tc._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and tv.check_user_id!=? and t.flag_stop='上缴停用' "
				+ " and t.flag='销毁申请中' and tv.check_flag='通过' and tv.review_flag is null and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql, params.toArray(),
				page);
		return list;
	}

	// 审核
	@Transactional
	public boolean check(String v_id, t_seal_destroy_verify verify) {

		t_seal_destroy_verify ver = super.findObjById(v_id,
				t_seal_destroy_verify.class);
		ver.setCheck_flag(verify.getCheck_flag());
		ver.setCheck_bank_id(WebTools.getCurrentBankId());
		ver.setCheck_note(verify.getCheck_note());
		ver.setCheck_time(getToday());
		ver.setCheck_user_id(WebTools.getCurrentUserId());
		super.updateObj(ver);

		if ("不通过".equals(ver.getCheck_flag())) {
			t_seal seal = super.findObjById(ver.getSeal_id(), t_seal.class);
			seal.setFlag("停用");
			seal.setFlag_stop("暂时停用");
			super.updateObj(seal);
		}
		return true;
	}

	// 审核
	@Transactional
	public boolean review(String v_id, t_seal_destroy_verify verify) {

		t_seal_destroy_verify ver = super.findObjById(v_id,
				t_seal_destroy_verify.class);
		ver.setReview_flag(verify.getReview_flag());
		ver.setReview_bank_id(WebTools.getCurrentBankId());
		ver.setReview_note(verify.getReview_note());
		ver.setReview_time(getToday());
		ver.setReview_user_id(WebTools.getCurrentUserId());
		super.updateObj(ver);

		if ("不通过".equals(ver.getCheck_flag())) {
			t_seal seal = super.findObjById(ver.getSeal_id(), t_seal.class);
			seal.setFlag("停用");
			seal.setFlag_stop("暂时停用");
			super.updateObj(seal);
		}

		return true;
	}

	@Transactional
	public void tes() {
		t_seal s = this.findObjById("4028814e4b77a0e3014b784e8db90027",
				t_seal.class);
		s.setTrue_code("859058997");
		this.updateObj(s);
	}

	public static void main(String[] args) {
		SealDestroyService s = SpringTools.getBean(SealDestroyService.class);
		// System.err.println(s.selectSealDestroyApplyList(
		// "4028814e4a8f6e0a014a8f933c1e0002", null, new Page()));
		s.tes();

	}

	// 取消上缴
	@Transactional
	public boolean cancleSeal(String seal_id) {
		t_seal seal = super.findObjById(seal_id, t_seal.class);
		seal.setFlag("停用");
		seal.setFlag_stop("暂时停用");
		super.updateObj(seal);
		return true;
	}

}
